<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i>后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item :to="{path:'/sys-admin/permission/admin'}">管理员管理</el-breadcrumb-item>
      <el-breadcrumb-item>添加管理员</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="用户名" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="ruleForm.password"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="ruleForm.nickname"></el-input>
      </el-form-item>
      <el-form-item label="头像" prop="avatar">
        <el-input v-model="ruleForm.avatar"></el-input>
      </el-form-item>
      <el-form-item label="手机号码" prop="phone">
        <el-input v-model="ruleForm.phone"></el-input>
      </el-form-item>
      <el-form-item label="电子邮箱" prop="email">
        <el-input v-model="ruleForm.email"></el-input>
      </el-form-item>
      <el-form-item label="简介" prop="description">
        <el-input v-model="ruleForm.description"></el-input>
      </el-form-item>
      <el-form-item label="是否启用" prop="enable">
        <el-switch
            v-model="ruleForm.enable"
            active-value="0"
            inactive-value="1"
            active-color="#13ce66"
            inactive-color="#ccc">
        </el-switch>
      </el-form-item>
      <el-form-item label="角色" prop="roleIds">
        <el-select v-model="ruleForm.roleIds" multiple placeholder="请选择">
          <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="warning" @click="$router.push('/sys-admin/product/album')">返回列表</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>
export default {
  name: "AlbumAddNewView",
  data() {
    return {
      options: [{
        value: '1',
        label: '系统管理员'
      }, {
        value: '2',
        label: '超级管理员'
      }, {
        value: '3',
        label: '商品管理员'
      }, {
        value: '4',
        label: '品牌管理员'
      }, {
        value: '5',
        label: '分类管理员'
      }, {
        value: '6',
        label: '相册管理员'
      }]
      ,
      ruleForm: {
        username: '测试管理员01',
        password:'123456',
        nickname:'99',
        avatar:'99',
        phone:'13454465666',
        email:'99@qq.com',
        description:'测试管理员的简介001',
        enable:0,
        roleIds:'1'
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4到15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 4, max: 15, message: '长度在 4到15 个字符', trigger: 'blur' }
        ],
        nickname: [],
        avatar: [],
        phone: [],
        email: [],
        description: [],
        enable: [],
        roleIds: [],
      }
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
            let url = "http://localhost:9181/admin/add-new";
            //使用qs框架将js对象转为formData数据格式
            let formData = this.qs.stringify(this.ruleForm);

            this.axios.post(url,formData).then((response) =>{
                 let jsonResult = response.data;
                 if(jsonResult.state==20000){
                    this.$message.success("添加管理员成功");
                    //重置表单
                    this.resetForm(formName);
                 }else {
                   this.$alert(jsonResult.message, '错误', {
                     confirmButtonText: '确定',
                     callback: action => {
                     }
                   });
                 }
            })

        } else { console.log('error submit!!');
          return false;

        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  }
}
</script>



<style scoped>

</style>